<?php
/**
 * The Controller
 *
 * @author Christer Damm
 **/
require_once 'restricted/main.conf.php';

$page = '';
if(isset($_REQUEST['p'])) {
  switch($_REQUEST['p']) {
    case 'link':
      $page = 'draggable_widgets';
      break;
    case 'select_widgets':
    default:
      $page = 'config_widgets';
      break;
  }
} else {
  $page = 'config_widgets';
}

require_once 'restricted/db.conf.php';
require_once "lib/db/connector.php";
$db = new Database($MYSQL_HOST, $MYSQL_USER, $MYSQL_PWD, $MYSQL_DB) or die("Couldn't connect to database");

if(file_exists("lib/business/$page.php")) {
  require_once "lib/business/$page.php";
  $layout = new Page($db);
  $lang = DEFAULT_LANGUAGE;
  if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    // if we have a localized set of strings, use them instead - this is a weak solution because it requires all strings to be translated, but it works
    if(file_exists('lib/l10n/'.substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2))) {
      $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
    }
  }
  // give the object some insight into what language to use
  $layout->set_language($lang);
  // give the object access to the POST data - this could be wrapped in another class, if needed
  $layout->add_post($_POST);
  // allow the object to filter the output
  ob_start(array($layout,'output_callback'));
  // output everything
  $layout->tinkle();
  // remove the filtering capabilities of the object
  ob_end_flush();
} else {
  echo 'Error occurred';
}

?>
